package org.momo.rpc.protocol;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.momo.rpc.protocol.constant.MessageType;
import org.momo.rpc.protocol.constant.ProtocolConstant;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class RpcMessage<T> {

    private Header header;

    /**
     * 消息体 RpcRequest/RpcResponse
     */
    private T data;

    @Data
    @NoArgsConstructor
    public static class Header {
        /**
         * 魔数
         */
        private byte magic = ProtocolConstant.MAGIC;

        /**
         * 版本号
         */
        private byte version = ProtocolConstant.VERSION;

        /**
         * body序列化方式
         */
        private byte serializer;

        /**
         * RPC消息类型
         */
        private byte messageType;

        /**
         * 状态码
         */
        private byte status;

        /**
         * id
         */
        private long id;

        /**
         * 消息体长度
         */
        private int length;
    }

    public static RpcMessage<RpcRequest> heartBeatRequest() {
        Header header = new Header();
        header.setMessageType(MessageType.HEARTBEAT_PING.getCode());
        return new RpcMessage<>(header, null);
    }

    public static RpcMessage<RpcResponse> heartBeatResponse() {
        Header header = new Header();
        header.setMessageType(MessageType.HEARTBEAT_PONG.getCode());
        return new RpcMessage<>(header, null);
    }
}
